package com.company.jncz.web.actions.topic;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.company.jncz.entity.Topic;
import com.company.jncz.service.TopicService;

@Controller
@RequestMapping("/topic/**")
public class TopicController {
	private TopicService topicService;
	@Autowired
	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	@RequestMapping(value="/list")
	public ModelMap list(){
		List<Topic> topics = this.topicService.getAllTopic();
		ModelMap modelmap = new ModelMap();
		modelmap.addAttribute(topics);
		return modelmap;
	}
	@RequestMapping(value="/create")
	public ModelMap create(){
		ModelMap mm = new ModelMap();
		return mm;
	}
	@RequestMapping(value="/save")
	public String save(HttpServletRequest request){
		Topic topic = new Topic();
		topic.setTitle(request.getParameter("title"));
		topic.setContent(request.getParameter("content"));
		this.topicService.saveTopic(topic);
		return "redirect:/success.do?path=topic";
	}
	
	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable String id){
		this.topicService.delete(id);
		return "redirect:/success.do?path=topic";
	}

}
